package com.czr.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czr.admin.pojo.Login;
import com.czr.admin.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public String login(Login login, Model model, HttpSession session){
        Login one = loginService.getOne(new QueryWrapper<Login>()
                .eq("username", login.getUsername())
                .eq("password", login.getPassword()), false);
        if (one != null) {
            session.setAttribute("logname", one.getUsername());
            return "redirect:/index.html";
        }else {
            model.addAttribute("msg", "用户名或者密码错误");
            return "/login";
        }
    }

    @RequestMapping("/outlogin")
    public String outlogin(HttpSession session, Model model){
        model.addAttribute("msg", "你已安全退出");
        session.invalidate();
        return "/login";
    }


}
